package com.erp.framework.controller.form;

import java.io.Serializable;
import java.util.HashMap;

import com.erp.framework.SimpleConstants;
import com.erp.framework.model.schema.TableSchema;
import com.erp.framework.util.CommonUtil;

public class MultiRow implements Serializable, SimpleConstants {

    private FormCommonTemplate template = new FormCommonTemplate();

    //public final static String MODE_CREATE = "MODE_CREATE";
    //public final static String MODE_MODIFY = "MODE_MODIFY";

    protected boolean deleteForever = false;
    protected String idx;
    protected boolean isVisible = true;
    protected boolean editable = true;
    protected boolean deletable = true;
    protected String rowMode = ROW_MODE_CREATE;
    protected String isDelete = "";
    protected boolean isDup = false;
    protected String updateVersion = "0";
    protected String isSelected = "";
    protected TableSchema ts;
    protected String tsName;

    protected HashMap errorMap = new HashMap();

    public void setIdx(String idx) {
        this.idx = idx;
    }

    public void setRowMode(String rowMode) {
        this.rowMode = rowMode;
    }

    public void setIsDelete(String isDelete) {
        if (isDelete.length() > 0)
            isVisible = false;
        else
            isVisible = true;
        this.isDelete = isDelete;
    }

    public void setUpdateVersion(String updateVersion) {
        this.updateVersion = updateVersion;
    }

    public String getIdx() {
        return this.idx;
    }

    public String getRowMode() {
        return this.rowMode;
    }

    public String getIsDelete() {
        return this.isDelete;
    }

    public String getUpdateVersion() {
        return this.updateVersion;
    }

    public void setModeModify() {
        setRowMode(ROW_MODE_MODIFY);
    }

    public void setModeCreate() {
        setRowMode(ROW_MODE_CREATE);
    }

    public boolean isReadOnly() {
        boolean rtn = false;
        if (this.rowMode != null && this.rowMode.equals(ROW_MODE_MODIFY)) {
            rtn = true;
        }

        return rtn;
    }

    public void addError(String property, String err) {
        this.errorMap.put(property, err);
    }

    public String getError(String property) {
        String rtnStr = "";
        try {
            rtnStr = (String) this.errorMap.get(property);
        } catch (Exception e) {
            rtnStr = "";
        }
        if (rtnStr == null) {
            rtnStr = "";
        }
        return rtnStr;
    }

    public void setIsDup(boolean isDup) {
        this.isDup = isDup;
    }

    public boolean getIsDup() {
        return this.isDup;
    }

    public void setDupMsg(String dd) {
    }

    public String getDupMsg() {
        String rtn = "";
        if (this.isDup) {
            rtn = "this record duplicated";
        }
        return rtn;
    }

    protected MultiChildrenRow childrenRow = null;

    public MultiChildrenRow getChildrenRow() {
        return this.childrenRow;
    }

    public void setChildrenRow(MultiChildrenRow childrenRow) {
        this.childrenRow = childrenRow;
    }

    public boolean isVisible() {
        return isVisible;
    }

    public void setVisible(boolean isVisible) {
        this.isVisible = isVisible;
    }

    public boolean isEditable() {
        return editable;
    }

    public void setEditable(boolean isEditable) {
        this.editable = isEditable;
    }

    public boolean isDeletable() {
        return deletable;
    }

    public void setDeletable(boolean isDeletable) {
        this.deletable = isDeletable;
    }

    public TableSchema getTs() {
        return ts;
    }

    public void setTs(TableSchema ts) {
        this.ts = ts;
    }

    public String getTsName() {
        return tsName;
    }

    public void setTsName(String tsName) {
        this.tsName = tsName;
    }

    public String getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(String isSelected) {
        this.isSelected = isSelected;
    }

    public boolean isDeleteForever() {
        return deleteForever;
    }

    public void setDeleteForever(boolean deleteForever) {
        this.deleteForever = deleteForever;
    }

    public boolean isDeleted() {
        return !CommonUtil.isEmpty(this.getIsDelete());
    }

    public void setDeleted(boolean deleted) {
        this.setIsDelete((deleted) ? "checked" : "");
    }

    protected FormCommonTemplate getTemplate() {
        return template;
    }

}
